home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW Related / MPW Script Tips 1.1.1 / Sample Scripts / CapFirst < prev    next >
Encoding:
Text File  |  1991-08-16  |  4.0 KB  |  124 lines  |  [TEXT/MPS ]

  1. #---------------------------------------------------------------------------------------------------------------------------------------------------
  2. #    CapFirst
  3. #    MPW Shell Script
  4. #    Written by Gina Cherry • August 16,1991
  5. #    Copyright:    © 1991 by Apple Computer, Inc., all rights reserved.
  6. #    
  7. #    Usage:    CapFirst [file]
  8. #    
  9. #    Function:
  10. #            CapFirst capitalizes the first character of each line of a text file.  CapFirst skips over blanks 
  11. #            and tabs.  If the first character is not a letter, no change occurs.
  12. #
  13. #    Note: CapFirst can be added to the Edit menu with the following command:
  14. #
  15. #                        AddMenu Edit 'CapFirst/6' 'CapFirst "{Active}"'
  16. #---------------------------------------------------------------------------------------------------------------------------------------------------
  17.  
  18. # If there is more than 1 parameter, write error message and exit script.
  19.     If {#} > 1                                                            
  20.         Echo "###Usage: {0} File"                             
  21.         Exit 1
  22.     End >> Dev:StdErr
  23.  
  24. # Do not exit on error.
  25.     Set Exit 0    
  26.  
  27. # Searches should be case sensitive.
  28.     Set CaseSensitive 1                                                
  29.  
  30. # If a parameter is given, File is set to the parameter.   Otherwise, File is the target file.
  31.     If {#} == 1                                                            
  32.         Set File "{1}"                                                    
  33.     Else
  34.         Set File "{Target}"
  35.     End
  36.  
  37. #    Record whether fName is already open.
  38.     Set fullName "`Files -i -f "{File}" ≥ Dev:Null`"
  39.     Set wasOpen `Evaluate "∂`Windows∂`" =~ /≈{fullName}≈/`
  40.             
  41. # Open input file.  Since diagnostic output from Open command is not needed, redirect it to bit bucket.
  42.     Open "{File}" ≥ Dev:Null        
  43.  
  44. # If input file does not exist, exit script.
  45.     If {Status} != 0                                                    
  46.         Echo "###{0}: {File} is not a valid file." 
  47.         Exit 1
  48.     End >> Dev:StdErr
  49.  
  50. # Set nLines to number of lines of text in input file.
  51.     Set nLines `count -l "{File}"`                            
  52.  
  53. # Mark currently selected text to restore state of window at end of script.
  54.     Mark -y § {0}.Selection    "{File}"                        
  55.  
  56. # Position cursor at beginning of input file.
  57.     Find • "{File}"                                                    
  58.  
  59. # Initialize counter variable.
  60.     Set Count 1                                                            
  61.  
  62. # Save value of NewWindowRect to restore later.
  63.     Set OldWindowRect "{NewWindowRect}"            
  64.  
  65. # Want the workspace window to be small.
  66.     Set NewWindowRect 0,0,100,100                    
  67.  
  68. # Open a {0}.Temporary file for workspace.
  69.     Open -n -t {0}.Temp                                                
  70.  
  71. # Restore value of NewWindowRect.
  72.     Set NewWindowRect "{OldWindowRect}"            
  73.  
  74. # Loop through lines in input file.
  75.     Loop
  76.     
  77.         # Break if no more lines.
  78.             Break If {Count} > {nLines}                            
  79.             
  80.         # Position cursor at beginning of the current line in input file.
  81.             Find Δ{Count} "{File}"                                    
  82.             
  83.         # Skip over taps and spaces, and select first character.
  84.             Find /[¬∂t ]/ "{File}"                                    
  85.             
  86.         # Increment the counter variable.
  87.             Evaluate Count += 1                                            
  88.             
  89.         # Check whether the selected character is a lowercase letter.
  90.         #    Note: Diagnostic output for the entire If statement is discarded because the If statement will 
  91.         #    produce an error message if the selected text in the input file is a parenthesis.  This occurs 
  92.         #    because the shell interprets the character as an unmatched parenthesis.
  93.             If "`catenate "{File}.§"`" =~ /[a-z]/ 
  94.                 # If so, change it to an uppercase letter and echo the change to the temporary file,  
  95.                 #    overwriting the file's contents.
  96.                     Translate a-z A-Z < "{File}.§" > {0}.Temp        
  97.                 
  98.                 # Select the entire temporary file (a single uppercase letter).
  99.                     Find •:∞ {0}.Temp                                            
  100.                 
  101.                 # Copy the contents of the temporary file to the clipboard.
  102.                     Copy § {0}.Temp                                                
  103.                 
  104.                 # Write the uppercase letter from the clipboard to the input file, overwriting  the 
  105.                 #    original lowercase letter.
  106.                     Paste § "{File}"                
  107.                 
  108.             End ≥ Dev:Null
  109.         
  110.     End
  111.  
  112. # Close the temporary file, don't save changes.        
  113.     Close -n {0}.Temp                                                    
  114.  
  115. # Select the text that was originally selected in the input file.
  116.     Find {0}.Selection "{File}"                                    
  117.  
  118. # Delete the marker from the input file.
  119.     Unmark {0}.Selection "{File}"                                
  120.  
  121. #    If the input file was not open to start with, close the file and save changes.    
  122.      If !"{wasOpen}"
  123.         Close -y "{File}"
  124.     End